package net.babeuf.form.factory 
{
	import net.babeuf.form.fields.AbstractField;
	import net.babeuf.form.fields.input.*;
	/**
	 * ...
	 * @author Damien Pegoraro
	 */
	public class MetaFieldFactory 
	{
		
		static private function getFieldFromType( type : String, o : Object ): AbstractField 
		{
			var out : AbstractField ; 		
			switch( type ) 
			{
				case "InputField" : 
					out = new InputField( o.target, o.min ? Number( o.min) : 0 , o.max ? Number(o.max) : 255 ) ; 						
				break ;			
				case "EmailField" : 
					out = new EmailField( o.target, o.min ? Number( o.min) : 0 , o.max ? Number(o.max) : 255 ) ; 						
				break ;		
				case "CreditCardField" :
					out = new CreditCardField( o.target ) ; 
				break ;
				case "DayField" :
					out = new DayField( o.target ) ; 
				break ;
				case "MonthField" :
					out = new MonthField( o.target ) ; 
				break ;
				case "NameField" :
					out = new NameField( o.target, o.min ? Number( o.min) : 0 , o.max ? Number(o.max) : 255 ) ; 	
				break ;
				case "NumericField" :
					out = new NumericField( o.target, o.min ? Number( o.min) : 0 , o.max ? Number(o.max) : 255 ) ; 	
				break ;
				case "PasswordField" :
					out = new PasswordField( o.target, o.min ? Number( o.min) : 0 , o.max ? Number(o.max) : 255 ) ; 	
				break ;
				case "PhoneField" :
					out = new PhoneField( o.target, o.mobile ? Boolean( o.mobile) : false , o.locale ? String(o.locale) : "fr" ) ; 	
				break ;
				case "PostalField" :
					out = new PostalField( o.target ) ; 	
				break ;
				case "YearField" :
					out = new YearField( o.target ) ; 	
				break ;
				case "CheckBoxField" : 
				case "ComponentCheckboxField" : 
				case "ComponentRadioField" : 
				case "MovieClipCheckboxField" : 
				case "ComboBelleField" : 
				case "ComboField" : 
				case "ComponentComboField" : 
				case "RadioGroup" : 
				case "AbstractField" : 
					throw new Error( "This field type is not supported yet in meta mode, but you can still use it in the regular way" ) ; 
				break ;
				default : 
					throw new Error( "Unknown field type" ) ; 
				break ;
			}
			return out  ;
		}
		
		
		static public function buildFieldFromObject( o : Object ) : AbstractField
		{
			var out : AbstractField = getFieldFromType( o.type, o ) ; 
			var type : String = o.type ; 
						
			if (  out ) 
			{
				if ( out is InputField )
				{
					if ( o.restrict ) InputField(out).setRestrict( String( o.restrict) ) ; 					
					if ( o.defaultLabel ) InputField(out).setDefaultLabel( String( o.defaultLabel) ) ; 						
				}
								
				if ( out is CreditCardField )
				{
					if ( o.allowMasterCard ) CreditCardField(out).allowMasterCard = Boolean( o.allowMasterCard ) ; 
					if ( o.allowVisa ) CreditCardField(out).allowVisa = Boolean( o.allowVisa ) ; 
					if ( o.allowAmericanExpress ) CreditCardField(out).allowAmericanExpress = Boolean( o.allowAmericanExpress ) ; 
					if ( o.allowDinersClub ) CreditCardField(out).allowDinersClub = Boolean( o.allowDinersClub ) ; 
					if ( o.allowDiscover ) CreditCardField(out).allowMasterCard = Boolean( o.allowDiscover ) ; 
					if ( o.allowEnRoute ) CreditCardField(out).allowMasterCard = Boolean( o.allowEnRoute ) ; 
					if ( o.allowJCB ) CreditCardField(out).allowMasterCard = Boolean( o.allowJCB ) ; 						
				}
								
				if ( o.notSent ) out.notSent = Boolean( o.notSent )  ; 
				if ( o.optional ) out.optional = Boolean( o.optional )  ; 
				if ( o.validateOnFocusOut ) out.validateOnFocusOut = Boolean( o.validateOnFocusOut )  ; 
				out.setErrors( String(o.minError),  String(o.maxError), String(o.emptyError), String(o.invalidError)) ; 			
			}			
			return out ;			
		}
		
	}

}